package com.ithong.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ithong.drug.entity.DrugInfo;
import com.ithong.drug.entity.Supplier;
import com.ithong.drug.service.DrugInfoService;
import com.ithong.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/drugInfo")
public class DrugInfoController {

    @Autowired
    private DrugInfoService drugInfoService;

    /**
     * 转向药品信息页面
     * @return
     */
    @RequestMapping
    public String index(){
        return "/druginfo";
    }

    /**
     * 分页查询药品信息列表
     */
    @RequestMapping("page")
    @ResponseBody
    public Object drugInfoQueryPage(String param,
                                    @RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize){
        try {
            IPage<DrugInfo> iPage = drugInfoService.selectDrugInfoPage(pageNum,pageSize,param);
            return ResultMapUtil.getMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 转向新增页面
     * @return
     */
    @RequestMapping("add")
    public String add(){
        return "/druginfoAdd";
    }

    /**
     * 添加一个药品信息
     */
    @RequestMapping("/drugInfoAdd")
    @ResponseBody
    public Object drugInfoAdd(DrugInfo drugInfo){
        try {
            drugInfo.setProductdate(new Date());
            int i = drugInfoService.addDrugInfo(drugInfo);
            return ResultMapUtil.getMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 转向编辑页面
     * @return
     */
    @RequestMapping("edit")
    public String edit(@RequestParam(name = "id",required = true) Integer id, Model model){
        DrugInfo drugInfo = drugInfoService.queryDrugInfoById(id);
        System.out.println(drugInfo);
        model.addAttribute("obj",drugInfo);
        return "/druginfoAdd";
    }

    /**
     * 修改一个药品信息
     */
    @RequestMapping("drugInfoEdit")
    @ResponseBody
    public Object drugInfoEdit(DrugInfo drugInfo){
        try {
            int i = drugInfoService.editDrugInfo(drugInfo);
            return ResultMapUtil.getMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 删除
     */
    @RequestMapping("drugInfoDel")
    @ResponseBody
    public Object DrugInfoDel(Integer id){
        try {
            int i = drugInfoService.delDrugInfo(id);
            return ResultMapUtil.getMapDel(id);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 返回所有药品
     */
    @RequestMapping("drugInfoList")
    @ResponseBody
    public Object supplierList(){
        List<DrugInfo> drugInfos = drugInfoService.queryDrugInfoList();
        return ResultMapUtil.getMapList(drugInfos);
    }

    /**
     * 转向药品保质期检查页面
     */
    @RequestMapping("warranty")
    public String warranty(){
        return "warranty";
    }

}
